package com.shopping.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shopping.bean.Goods;
import com.shopping.service.GoodsService;
import com.shopping.service.impl.GoodsServiceImpl;

@WebServlet("/getGoodsMsg.do")
public class GetGoodsMsgServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int gId = Integer.parseInt(request.getParameter("gId"));
		System.out.println(gId);
		GoodsService gs = new GoodsServiceImpl();
		Goods goods = gs.selectGoodsById(gId);
		ObjectMapper om = new ObjectMapper();
		response.setCharacterEncoding("UTF-8"); //设置 后端给前端传回的编码格式为utf-8
		response.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		response.getWriter().print(om.writeValueAsString(goods));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
